﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using Entities;
using System.Collections.Generic;

public partial class oprrecord : System.Web.UI.Page
{
    public int sign = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
            DAL.WorkDALs workdals = new DAL.WorkDALs();
            GridView1.DataSource = workdals.GetList();
            string[] keys ={ "Work_id" };
            this.GridView1.DataKeyNames = keys;
            GridView1.DataBind();

            DAL.UserDALs userdals = new DAL.UserDALs();
            System.Collections.Generic.IList<User> list = userdals.GetList();
            System.Collections.Generic.IList<User> lst  = new System.Collections.Generic.List<User>();
            User user1 = new User();
            user1.User_name = "全部";
            lst.Add(user1);
            foreach (User user2 in list)
            {
               lst.Add(user2);
            }
            this.checkuser.DataSource = lst;
            this.checkuser.DataBind();


            DAL.ServerDALs server1 = new DAL.ServerDALs();
            System.Collections.Generic.IList<Server> list1 = server1.GetList();
            System.Collections.Generic.IList<Server> lst1 = new System.Collections.Generic.List<Server>();
            ListItem it1 = new ListItem();
            Server server = new Server();
            server.Name = "全部";
            lst1.Add(server);
            foreach (Server server11 in list1)
            {
                lst1.Add(server11);
            }

            this.server.DataSource = lst1;  
            this.server.DataBind();


        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DAL.WorkDALs workdals = new DAL.WorkDALs();
        GridView1.DataSource = workdals.GetList();
        string[] keys ={ "Work_id" };
        this.GridView1.DataKeyNames = keys;
        this.GridView1.DataBind();

        DAL.UserDALs userdals = new DAL.UserDALs();
        this.checkuser.DataSource = userdals.GetList();
        this.checkuser.DataBind();

        DAL.ServerDALs server1 = new DAL.ServerDALs();
        this.server.DataSource = server1.GetList();
        this.server.DataBind();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        DAL.WorkDALs workdals = new DAL.WorkDALs();
        IList<Work> list = new List<Work>();

        if (server.SelectedValue == "0" && checkuser.SelectedValue != "0")
        {
            list = workdals.GetListByCondition(null, int.Parse(this.status.SelectedValue), int.Parse(this.checkuser.SelectedValue));
        }
        else if (server.SelectedValue != "0"  && checkuser.SelectedValue == "0")
        {
            list = workdals.GetListByCondition(int.Parse(this.server.SelectedValue), int.Parse(this.status.SelectedValue),null);
        }
        else if (server.SelectedValue == "0" && checkuser.SelectedValue == "0")
        {
            list = workdals.GetListByCondition(null, int.Parse(this.status.SelectedValue),null);
        }
        else if (server.SelectedValue != "0" && checkuser.SelectedValue != "0")
        {
            list = workdals.GetListByCondition(int.Parse(this.server.SelectedValue), int.Parse(this.status.SelectedValue), int.Parse(this.checkuser.SelectedValue));
        }

        IList<Work> lst = new System.Collections.Generic.List<Work>();

        foreach (Work work in list) 
        {
           if(!lst.Contains(work))
           {
               lst.Add(work);
           }
        }
        
        this.GridView1.DataSource = lst;
        string[] keys ={ "Work_id" };
        this.GridView1.DataKeyNames = keys;
        GridView1.DataBind();

        Response.Redirect("oprrecord.aspx");
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int workId = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);
        this.Response.Redirect("addOprRecord.aspx?work_id="+workId);
    }


    protected string DGFormatSex(string xb)
    {
        if (xb.Trim() == "0")
            return "无意外";
        else if (xb.Trim() == "1")
            return "失败回滚";
        else if (xb.Trim() == "2")
            return "服务器5分钟故障";
        else if (xb.Trim() == "3")
            return "服务器半小时故障";
        else if (xb.Trim() == "4")
            return "服务器一小时故障";

        return "";

    }

    protected string DGFormatSex1(string xb)
    {
        if (xb.Trim() == "0")
            return "日常web发布";
        else if (xb.Trim() == "1")
            return "日常维护";
        else if (xb.Trim() == "2")
            return "紧急发布";

        return "";
    }

    protected string DGFormatSex2(int xb)
    {
        DAL.FlowDALs flowdals = new FlowDALs();
        Flow flow = flowdals.GetByID(xb);
        return flow.Name;
    }





    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int work_id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        DAL.WorkDALs workdals = new DAL.WorkDALs();

        workdals.DeleteWorks(work_id);


        GridView1.DataSource = workdals.GetList();
        string[] keys ={ "Work_id" };
        this.GridView1.DataKeyNames = keys;
        this.GridView1.DataBind();

        DAL.UserDALs userdals = new DAL.UserDALs();
        this.checkuser.DataSource = userdals.GetList();
        this.checkuser.DataBind();

        DAL.ServerDALs server1 = new DAL.ServerDALs();
        this.server.DataSource = server1.GetList();
        this.server.DataBind();
    }


    public string GetOperatePerson(int xb)
    {
        DAL.Work_userDALs dals = new Work_userDALs();

        IList<Work_user> work_users = dals.GetByWorkId(xb);

        string result = "";
        foreach (Work_user work_user in work_users)
        {
            DAL.UserDALs userdals = new UserDALs();
            User user = userdals.GetByID(work_user.User_id);
            result += user.User_name + " ";
        }
        return result;
    }
}
